package com.ruoyi.basebill.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.OmsReceiptbillentry;
import com.ruoyi.basebill.mapper.OmsReceiptbillMapper;
import com.ruoyi.basebill.domain.OmsReceiptbill;
import com.ruoyi.basebill.service.IOmsReceiptbillService;

/**
 * OMS收货确认Service业务层处理
 * 
 * @author ptt
 * @date 2023-02-10
 */
@Service
public class OmsReceiptbillServiceImpl implements IOmsReceiptbillService 
{
    @Autowired
    private OmsReceiptbillMapper omsReceiptbillMapper;

    @Autowired
    private OmsReceiptbillServiceImpl omsReceiptbillService;

    /**
     * 查询OMS收货确认
     * 
     * @param id OMS收货确认主键
     * @return OMS收货确认
     */
    @Override
    public OmsReceiptbill selectOmsReceiptbillById(Long id)
    {
        return omsReceiptbillMapper.selectOmsReceiptbillById(id);
    }

    public OmsReceiptbill selectOmsReceiptbillSummaryEntryById(Long id)
    {
        return omsReceiptbillMapper.selectOmsReceiptbillSummaryEntryById(id);
    }


    /**
     * 查询OMS收货确认列表
     * 
     * @param omsReceiptbill OMS收货确认
     * @return OMS收货确认
     */
    @Override
    public List<OmsReceiptbill> selectOmsReceiptbillList(OmsReceiptbill omsReceiptbill)
    {
        return omsReceiptbillMapper.selectOmsReceiptbillList(omsReceiptbill);
    }

    @Override
    public List<OmsReceiptbill> selectOmsReceiptbillDetailList(OmsReceiptbill omsReceiptbill) {
        return omsReceiptbillMapper.selectOmsReceiptbillDetailList(omsReceiptbill);
    }

    @Override
    public List<OmsReceiptbill> selectOmsReceiptbillBySumbill(OmsReceiptbill omsReceiptbill) {
        String sumbillNumber = omsReceiptbill.getSumbillNumber();
        String substring = sumbillNumber.substring(0, 6);
        if ("HZXXCK".equals(substring)){
            return omsReceiptbillMapper.selectOmsReceiptbillBySumSalebill(omsReceiptbill);
        }else if ("HZCGRK".equals(substring)){
            return omsReceiptbillMapper.selectOmsReceiptbillBySumPurbill(omsReceiptbill);
        }else if ("HZKCDB".equals(substring)){
            return omsReceiptbillMapper.selectOmsReceiptbillBySumStocbill(omsReceiptbill);
        }
        return null;
    }

    /**
     * 新增OMS收货确认
     * 
     * @param omsReceiptbill OMS收货确认
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsReceiptbill(OmsReceiptbill omsReceiptbill)
    {
        int rows = omsReceiptbillMapper.insertOmsReceiptbill(omsReceiptbill);
        omsReceiptbillService.insertOmsReceiptbillentry(omsReceiptbill);
        return rows;
    }

    /**
     * 修改OMS收货确认
     * 
     * @param omsReceiptbill OMS收货确认
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsReceiptbill(OmsReceiptbill omsReceiptbill)
    {
        if (omsReceiptbill.getOmsReceiptbillentryList() != null && omsReceiptbill.getOmsReceiptbillentryList().size() != 0){
            omsReceiptbillMapper.deleteOmsReceiptbillentryByParentid(omsReceiptbill.getId());
            omsReceiptbillService.insertOmsReceiptbillentry(omsReceiptbill);
        }
        return omsReceiptbillMapper.updateOmsReceiptbill(omsReceiptbill);
    }

    public int updateOmsReceiptbillHead(OmsReceiptbill omsReceiptbill){
        return omsReceiptbillMapper.updateOmsReceiptbill(omsReceiptbill);
    }


    /**
     * 批量删除OMS收货确认
     * 
     * @param ids 需要删除的OMS收货确认主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsReceiptbillByIds(Long[] ids)
    {
        omsReceiptbillMapper.deleteOmsReceiptbillentryByParentids(ids);
        return omsReceiptbillMapper.deleteOmsReceiptbillByIds(ids);
    }

    /**
     * 删除OMS收货确认信息
     * 
     * @param id OMS收货确认主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsReceiptbillById(Long id)
    {
        omsReceiptbillMapper.deleteOmsReceiptbillentryByParentid(id);
        return omsReceiptbillMapper.deleteOmsReceiptbillById(id);
    }

    /**
     * 新增OMS收货确认分录信息
     * 
     * @param omsReceiptbill OMS收货确认对象
     */
    public void insertOmsReceiptbillentry(OmsReceiptbill omsReceiptbill)
    {
        List<OmsReceiptbillentry> omsReceiptbillentryList = omsReceiptbill.getOmsReceiptbillentryList();
        Long id = omsReceiptbill.getId();
        if (StringUtils.isNotNull(omsReceiptbillentryList))
        {
            List<OmsReceiptbillentry> list = new ArrayList<OmsReceiptbillentry>();
            for (OmsReceiptbillentry omsReceiptbillentry : omsReceiptbillentryList)
            {
                omsReceiptbillentry.setParentid(id);
                list.add(omsReceiptbillentry);
            }
            if (list.size() > 0)
            {
                omsReceiptbillMapper.batchOmsReceiptbillentry(list);
            }
        }
    }

    @Override
    public List<OmsReceiptbill> selectPushDownBill() {
        return omsReceiptbillMapper.selectPushDownBill();
    }

    @Override
    public List<OmsReceiptbill> selectPushDownOutSaleBill() {
        return omsReceiptbillMapper.selectPushDownOutSaleBill();
    }
}
